﻿using System;
using System.Net;
using SCG.SolarVengeanceEngine.Orders;
using SCG.General;

namespace SCG.SolarVengeanceEngine
{
    //Represents an order for a StarShip
    public abstract class StarShipOrder : SVObject
    {
        //constructor
        public StarShipOrder(SVGame game, OrderType ot)
            : base(game)
        {
            OrderType = ot;
        }

        //The order type
        public OrderType OrderType { get; set; }

        //persistence
        public virtual string Persist()
        {
            SVTokenizer tok = new SVTokenizer(Game);
            tok.AppendToken(OrderType);
            return tok.Result;
        }

        //get the order's target point
        public virtual SCGPoint TargetPoint
        {
            get
            {
                return new SCGPoint(0, 0);
            }
        }

        //is it targeting a StarShip?
        public virtual bool OrderTargetsStarShip
        {
            get
            {
                return false;
            }
        }

        //get the starship that the order targets
        public virtual StarShip StarShipTargeted
        {
            get
            {
                return null;
            }
        }

        //should it draw the order line?
        private static SCGColor MoveColor = new SCGColor(48, 0, 255, 255);
        private static SCGColor TargetColor = new SCGColor(80, 255, 0, 0);
        private static SCGColor DefendColor = new SCGColor(80, 0, 128, 0);
        private static SCGColor EscortColor = new SCGColor(80, 0, 0, 255);
        private static SCGColor PatrolColor = new SCGColor(80, 255, 0, 255);
        private static SCGColor TransportPen = new SCGColor(128, 128, 128, 0);
        public virtual SCGColor LineColor
        {
            get
            {
                switch (OrderType)
                {
                    case OrderType.Move:
                        return MoveColor;
                    case OrderType.TargetMapObject:
                    case OrderType.TargetStarShip:
                    case OrderType.TargetStarSystem:
                    case OrderType.BoardCarrier:
                        return TargetColor;
                    case OrderType.Defend:
                    case OrderType.DefendPoint:
                        return DefendColor;
                    case OrderType.Escort:
                        return EscortColor;
                    case OrderType.Patrol:
                        return PatrolColor;
                    case OrderType.Transport:
                    case OrderType.Unload:
                        return TransportPen;
                    default:
                        return SCGColor.Transparent;
                }
            }
        }

        //create appropriate instance
        public static StarShipOrder CreateInstance(SVGame game, string s)
        {
            SVTokenizer tok = new SVTokenizer(game, s);
            OrderType ot = tok.TokenOrderType;         
            switch (ot)
            {
                case OrderType.Move:
                case OrderType.DefendPoint:
                    return new PointOrder(game, ot, tok);                 
                case OrderType.TargetStarSystem:
                case OrderType.Defend:
                case OrderType.TargetStarShip:
                case OrderType.Escort:
                case OrderType.Unload:
                case OrderType.TargetMapObject:
                    return new MapObjectOrder(game, ot, tok);    
                case OrderType.Patrol:
                    return new PatrolOrder(game, ot, tok);
                case OrderType.Transport:
                    return new TransportOrder(game, ot, tok);
                default:
                    throw new ArgumentException("Unknown OrderType: " + ot);
            }
        }
    }
}
